package io;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CharacterStreamDemo {
    public static void main(String[] args) {
        // 字符流适合处理文本文件
        String sourceText = "source.txt";
        String targetText = "target.txt";

        // 使用try-with-resources自动关闭流
        try (FileReader reader = new FileReader(sourceText);
             FileWriter writer = new FileWriter(targetText)) {

            // 字符缓冲区（一次读取1024个字符）
            char[] buffer = new char[1024];
            int charsRead;

            System.out.println("开始复制文本...");
            while ((charsRead = reader.read(buffer)) != -1) {
                // 写入读取的字符
                writer.write(buffer, 0, charsRead);
            }
            System.out.println("文本复制完成！");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
